﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightWcfDataServiceProject.ServiceReference1;
using System.Data.Services.Client;

namespace SilverlightWcfDataServiceProject
{
    public partial class ManageItem : UserControl
    {
        InvEntities1 Inv;
        DataServiceCollection<Item> Items;
        public ManageItem()
        {
            InitializeComponent();
            Inv = new InvEntities1(new Uri("http://localhost:51525/WcfDataService1.svc"));
            var ItemQ = from i in Inv.Items
                        select i;
            Items = new DataServiceCollection<Item>();
            Items.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(Items_LoadCompleted);
            Items.LoadAsync(ItemQ);
        }

        void Items_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            listBoxItems.ItemsSource = Items;
            listBoxItems.DisplayMemberPath = "Name";
            //gridItem.DataContext = Items.First<Item>();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

            // Do not load your data at design time.
            // if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            // {
            // 	//Load your data here and assign the result to the CollectionViewSource.
            // 	System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
            // 	myCollectionViewSource.Source = your data
            // }
        }

        private void listBoxItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            gridItem.DataContext = (Item)listBoxItems.SelectedItem;
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            Inv.BeginSaveChanges(i => MessageBox.Show("changed"), ((Item)listBoxItems.SelectedItem));
        }

        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            AddItem AdditemForm = new AddItem();
            AdditemForm.Show();
            AdditemForm.Closed += new EventHandler(AdditemForm_Closed);
        }

        void AdditemForm_Closed(object sender, EventArgs e)
        {
            AddItem AddItemForm=(AddItem)sender;
            if (AddItemForm.itm != null)
            {
                Items.Add(AddItemForm.itm);
                Inv.BeginSaveChanges(i => MessageBox.Show("Added"), Items);
            }
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            Items.Remove((Item)listBoxItems.SelectedItem);
            Inv.BeginSaveChanges(i => MessageBox.Show("deleted"), Items);
        }
    }
}
